package com.example.llademo.util;

import com.alibaba.excel.EasyExcel;
import com.example.llademo.entity.ADSPosition;
import com.example.llademo.excel.AdsDataListener;
import com.example.llademo.model.ADSPositionModel;
import com.example.llademo.model.dto.ExcelReadResult;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.File;
import java.util.List;

/**
 * @author zhangqi
 * @date 2024/4/2
 * @time 09:42
 * @description
 */
@Slf4j
@Component
public class LlaExcelUtil {

    @Value("${lla.excelpath}")
    private String llaExcelPath;
    @Autowired
    private LlaIOUtil llaIOUtil;

    public void simpleRead(){
//        log.info("simple read");
        List<File> files = llaIOUtil.readFilesFromResource(llaExcelPath, ".csv");
        if (CollectionUtils.isNotEmpty(files)){
            File adsFile = files.get(0);
            AdsDataListener adsDataListener = new AdsDataListener();
//            log.info("adsFile name:{}",adsFile.getName());
            EasyExcel.read(adsFile, ADSPositionModel.class,adsDataListener)
                    .sheet()
                    .headRowNumber(1)
                    .doRead();
            List<ADSPosition> list = adsDataListener.getList();
            log.info("list.size:{}",list.size());
        }else{
            throw new RuntimeException("can not read ads file.");
        }
    }

    //读取ads以及返回结果
    public ExcelReadResult simpleReadResult(){
//        log.info("simple read");
        List<File> files = llaIOUtil.readFilesFromResource(llaExcelPath, ".csv");
        if (CollectionUtils.isNotEmpty(files)){
            File adsFile = files.get(0);
            AdsDataListener adsDataListener = new AdsDataListener();
//            log.info("adsFile name:{}",adsFile.getName());
            EasyExcel.read(adsFile, ADSPositionModel.class,adsDataListener)
                    .sheet()
                    .headRowNumber(1)
                    .doRead();
            List<ADSPosition> list = adsDataListener.getList();
            log.info("list.size:{}",list.size());
            return ExcelReadResult.builder().takeOffTime(adsDataListener.getTakeOffTime())
                    .landingTime(adsDataListener.getLandingTime())
                    .adsList(list)
                    .build();
        }else{
            throw new RuntimeException("can not read ads file.");
        }

    }

}
